#include "stdafx.h"

#include "Logger.h"
#include "log4cplus/fileappender.h"
#include "log4cplus/logger.h"
#include "log4cplus/loglevel.h"
#include "log4cplus/layout.h"

using namespace log4cplus;

namespace ZeEngine
{
    log4cplus::Logger Logger::m_Logger = log4cplus::Logger::getInstance("Sidey");

    bool Logger::m_InstanceFlag = false;

    ZeEngine::Logger* Logger::m_Instance = NULL;

    Logger* Logger::getInstance()
    {
        if(!m_InstanceFlag)
        {
            m_Instance = new Logger();
            m_Instance->initializeLogger();
            m_InstanceFlag = true;
            return m_Instance;
        }        
        else
        {
            return m_Instance;
        }
    }


    void Logger::initializeLogger()
    {
        #ifdef _DEBUG
            m_Logger.setLogLevel(INFO_LOG_LEVEL);
        #else
            m_Logger.setLogLevel(WARN_LOG_LEVEL);
        #endif
            
        m_Logger.log(INFO_LOG_LEVEL, "Initializing Logger");  

        //Create a simple layout for the log file eg: <Log Level> - <Error Message>
        std::string wLogFile = "BonHomme.log";
        SharedAppenderPtr myAppender(new FileAppender(wLogFile));
        myAppender->setName("Appender" + wLogFile);  

        std::auto_ptr<Layout> myLayout = std::auto_ptr<Layout>(new log4cplus::SimpleLayout());
        myAppender->setLayout(myLayout);

        m_Logger.addAppender(myAppender);     

        m_Logger.log(INFO_LOG_LEVEL, "Logger initialized");   
    }

    void Logger::logError(const log4cplus::tstring& iClassName, const log4cplus::tstring& iFunctionName, const log4cplus::tstring& iMessage)
    {
        m_Logger.log(ERROR_LOG_LEVEL, iClassName + "::" + iFunctionName + "()--> " + iMessage);  
    }

    void Logger::logInformation(const log4cplus::tstring& iClassName, const log4cplus::tstring& iFunctionName, const log4cplus::tstring& iMessage)
    {
        m_Logger.log(INFO_LOG_LEVEL, iClassName + "::" + iFunctionName + "()--> " + iMessage); 
    }

    void Logger::logWarning(const log4cplus::tstring& iClassName, const log4cplus::tstring& iFunctionName, const log4cplus::tstring& iMessage)
    {
        m_Logger.log(WARN_LOG_LEVEL, iClassName + "::" + iFunctionName + "()--> " + iMessage); 
    }

    void Logger::logError(const log4cplus::tstring& iMessage)
    {
        m_Logger.log(ERROR_LOG_LEVEL, iMessage); 
    }

    void Logger::logInformation(const log4cplus::tstring& iMessage)
    {
        m_Logger.log(INFO_LOG_LEVEL, iMessage); 
    }

    void Logger::logWarning(const log4cplus::tstring& iMessage)
    {
        m_Logger.log(WARN_LOG_LEVEL, iMessage); 
    }
}
